package postpc.powerreminders.com.events;

import postpc.powerreminders.com.R;
import postpc.powerreminders.com.db.RotgDatabase;
import postpc.powerreminders.com.location.RotgLocationManager;
import postpc.powerreminders.com.systemEvents.IncomingCallReceiver;
import postpc.powerreminders.com.systemEvents.OutgoingCallReceiver;
import postpc.powerreminders.com.systemEvents.SystemEvents;
import postpc.powerreminders.com.ui.activities.MainActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;

public class RotgEventManager {

	private static final String TAG = "ROTG_EVENT_MANAGER";

	/*
	 * Reference to the location Manager.
	 */
	RotgLocationManager locationManager;	

	/*
	 * Reference to the main activity for services.
	 */	
	Activity mainActivity;

	/*
	 * Reference to the data base.
	 */	
	RotgDatabase dataBase;

	/*
	 * Handler to the system events.
	 */
	SystemEvents systemEvents;

	/*
	 * Handler for the incoming call receiver
	 */
	private IncomingCallReceiver incomingCall;
	
	/*
	 * Handler for the outgoing call receiver
	 */
	private OutgoingCallReceiver outgoingCall;

	/**
	 * 
	 * @param ma
	 */
	public RotgEventManager(MainActivity ma){

		// Save reference to the main activity
		this.mainActivity = ma;

		// Create the Data Base 
		Log.i(TAG, "Creating Database services");
		this.dataBase = new RotgDatabase(this.mainActivity);
		this.dataBase.open();

		// Create the location manager
		this.locationManager = new RotgLocationManager(this, ma, this.dataBase);
		
		// Create the system events manager.
		this.systemEvents = new SystemEvents(this, dataBase, mainActivity);
		this.setIncomingCall(new IncomingCallReceiver(dataBase));
		this.setOutgoingCall(new OutgoingCallReceiver(dataBase));
	}

	/*
	 * Notify user with an Alert dialog in the middle of the screen.
	 */
	public void notifyUserDialog(String notification) {
		Log.i(TAG, "Notify User: " + notification);
		Builder alert =  new AlertDialog.Builder(this.mainActivity);
		alert.setTitle("Reminder")
		.setMessage(notification)
		.setNeutralButton("Got it", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		})
		.create()
		.show();
	}

	/*
	 * Notify the user with a notifications bar dialog.
	 */
	public void notifyUserNotification(String notificationText) {

		NotificationManager notificationManager = (NotificationManager) 
				this.mainActivity.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.notification_icon, 
				notificationText, 
				System.currentTimeMillis());

		CharSequence contentTitle = "Power Reminders";
		Intent notificationIntent = new Intent();
		PendingIntent contentIntent = PendingIntent.getActivity(this.mainActivity, 0, notificationIntent, 0);

		notification.setLatestEventInfo(this.mainActivity.getApplicationContext(), contentTitle, notificationText, contentIntent);
		notification.defaults |= Notification.DEFAULT_SOUND;
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		notification.defaults |= Notification.FLAG_AUTO_CANCEL;

		notificationManager.notify(1, notification);
	}

	public void onDestroy() {
		
		// Close location manager
		this.locationManager.onDestroy();
		
		// Close System Events
		this.systemEvents.onDestroy();
		
		// Close call receiver
		incomingCall.abortBroadcast();
		outgoingCall.abortBroadcast();
	}

	public void notifyReminderGPS(String message) {
		notifyUserNotification(message);
	}

	public void notifyEventWifi(String message) {
		Log.i("IMPORTANT", "Got Message" + message);
		notifyUserNotification(message);
	}

	public void notifyReminderEvent(String message) {
		notifyUserNotification(message);
	}

	public IncomingCallReceiver getIncomingCall() {
		return incomingCall;
	}

	public void setIncomingCall(IncomingCallReceiver incomingCall) {
		this.incomingCall = incomingCall;
	}

	public OutgoingCallReceiver getOutgoingCall() {
		return outgoingCall;
	}

	public void setOutgoingCall(OutgoingCallReceiver outgoingCall) {
		this.outgoingCall = outgoingCall;
	}
}